<html>
<body>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>

<script type="text/javascript">
    FB.init("da858c537480037db939c5e37147264b", "xd_receiver.htm");
</script>

<script type="text/javascript" src="lib/gears_init.js"></script>
<script>

function init() {
if (!window.google || !google.gears) {
alert('not installed');
return;
}


function successCallback(p) {
var address = p.gearsAddress.city + ', '
+ p.gearsAddress.region + ', '
+ p.gearsAddress.country + ' ('
+ p.latitude + ', '
+ p.longitude + ')';

    var longitude = document.getElementById('longitude');

    longitude.value = p.longitude;

    var latitude = document.getElementById('latitude');

    latitude.value = p.latitude;

    var city = document.getElementById('city');

    city.value = p.gearsAddress.city;

    var region = document.getElementById('region');

    region.value = p.gearsAddress.region;

    var country = document.getElementById('country');

    country.value = p.gearsAddress.country;

    var channel = document.getElementById('channelName');

    channel.value = p.gearsAddress.city;

//alert(address);
    
    var distance = 0;

    var minDistance = 1000;

    var finalChannel;

    for(var i=0;i<7;++i)
        {
            var channelName = document.getElementById('channel'+i);

            var channelLongitude = document.getElementById('channelLongitude'+i);

            var channelLatitude = document.getElementById('channelLatitude'+i);

            var longDistance = longitude.value - channelLongitude.value;

            var latDistance = latitude.value - channelLatitude.value;

            distance = Math.sqrt(longDistance*longDistance  +  latDistance*latDistance);

            if(distance<minDistance)
                {
                    minDistance = distance;
                    finalChannel = channelName.value;
                }
        }

        //alert(distance);
        //alert(finalChannel);

        channel.value = finalChannel;

    aryClassElements.length = 0;

    getElementsByClassName( 'FB_Link', document.body );

    for ( var i = 0; i < aryClassElements.length; i++ ) {
        var name = document.getElementById("userName");
        var link = aryClassElements[i];
        name.value = link.innerHTML;
        document.getElementById("testForm").submit();
    }

}


function errorCallback(err) {
return;
}

try {
var geolocation = google.gears.factory.create('beta.geolocation');
geolocation.getCurrentPosition(successCallback,
errorCallback,
{ enableHighAccuracy: true,
gearsRequestAddress: true });
} catch (e) {
return;
}

}
</script>

<script lanuage="JavaScript">
var aryClassElements = new Array();

function doSomething() {
    init()
}

function getElementsByClassName( strClassName, obj ) {
    if ( obj.className == strClassName ) {
        aryClassElements[aryClassElements.length] = obj;
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        getElementsByClassName( strClassName, obj.childNodes[i] );
}
</script>
<?php
   		// List containing the custom channels:
		$channels = null;
		require('lib/data/channels.php');
		$channelLatitude = null;
		require('lib/data/channels.php');
		$channelLongitude = null;
		require('lib/data/channels.php');
?>


        <?php
        foreach($channels as $key=>$value) {
         echo '<input type="hidden" name="channel',$key,'" id="channel',$key,'" value="', $value, '"/>';
        }
        foreach($channelLatitude as $key=>$value) {
         echo '<input type="hidden" name="channelLatitude',$key,'" id="channelLatitude',$key,'" value="', $value, '"/>';
        }
        foreach($channelLongitude as $key=>$value) {
         echo '<input type="hidden" name="channelLongitude',$key,'" id="channelLongitude',$key,'" value="', $value, '"/>';
        }
        ?>
<form name="test" action="index.php" id="testForm" method="post">
    <input type="hidden" name="userName" id="userName"/>
    <input type="hidden" name="longitude" id="longitude"/>
    <input type="hidden" name="latitude" id="latitude"/>
    <input type="hidden" name="city" id="city"/>
    <input type="hidden" name="region" id="region"/>
    <input type="hidden" name="country" id="country"/>
    <input type="hidden" name="channelName" id="channelName"/>
    <div id="testid">
        <fb:name uid="loggedinuser" useyou="false" linked="true"></fb:name>
    </div>
    <input type="button" value="Log In to Chat" onClick="javascript:doSomething()"/>
</form>

</body>
</html>